package teste;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;


public class RotationTest extends JFrame {
	public RotationTest() {
		super("Animation of rotation about center");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 400);

		add(new MyComponent());

		setVisible(true);
	}

	public static void main(String args[]) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new RotationTest();
			}
		});
	}

	private class MyComponent extends JComponent implements ActionListener {
		private Timer timer;
		private Rectangle.Double r = new Rectangle.Double(100, 100, 50, 50);
		private double theta = 0;

		public MyComponent() {
			super();
			timer = new Timer(1000 / 24, this);
			timer.start();
		}

		public void actionPerformed(ActionEvent e) {
			theta += Math.PI / 18;
			if (theta >= Math.PI * 2) {
				theta = 0;
			}
			repaint();
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);

			Graphics2D g2 = (Graphics2D) g;
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g2.rotate(theta, 125, 125);
			g2.setColor(Color.GRAY);
			g2.fill(r);
		}
	}
}